package com.aishua.fire.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.MD5Util;
import com.aishua.fire.common.constant.DictConstants;
import com.aishua.fire.common.constant.SoftdelConstants;
import com.aishua.fire.entity.account.AccountBatchInfo;
import com.aishua.fire.mapper.dto.AccteamList;
import com.aishua.fire.entity.account.AddAccountInfo;
import com.aishua.fire.entity.account.UpdateAccountInfo;
import com.aishua.fire.entity.addressBook.AddAddressBookInfos;
import com.aishua.fire.mapper.dto.AccountData;
import com.aishua.fire.jpa.AccountAccountteamDao;
import com.aishua.fire.jpa.AccountDao;
import com.aishua.fire.jpa.entity.Account;
import com.aishua.fire.jpa.entity.Accountteam;
import com.aishua.fire.mapper.AccountMapper;
import com.aishua.fire.mapper.dto.AccountList;
import com.aishua.fire.service.AccountService;
import com.aishua.fire.service.AccountteamService;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {

	@Autowired
    private AccountDao accountDao;
	@Autowired
	private AccountAccountteamDao accountAteamDao;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private AccountteamService accountteamService;
	
	
	/**
	 * 增加账户信息
	 */
	@Transactional
	public String save(AddAccountInfo loginAccount) 
	{
		if (accountDao.findByUserName(loginAccount.getUserName()) !=null) 
		{
			//代表该账户已经存在,无法继续添加
			return "账户已经存在";
		}
		if (accountDao.findByUserPhone(loginAccount.getPhone()) !=null) 
		{
			//代表该手机号已经存在,无法继续添加
			return "手机号已经存在";
		}
		//添加账户表
		Account account = new Account(loginAccount.getUserName(), loginAccount.getUserPassword(), loginAccount.getAliasName(),
				 loginAccount.getPhone(), new Date(), "0", loginAccount.getEmailAddr());
		account.setLastLoginTime(new Date());
		account.setAccountStatus(DictConstants.ACCOUNT_YES);
		account.setStatus(SoftdelConstants.DEL_N);
		Account save = accountDao.saveAndFlush(account);
		//根据手机号查询当前用户是否被添加为通讯录员工
		AddAddressBookInfos addressBook = accountMapper.getaddressBookByPhone(loginAccount.getPhone());
		if(addressBook != null )
		{
			addressBook.setLogin_id(save.getLoginId()+"");
			accountMapper.updateAddressBookInfo(addressBook);
		}
		return "";
	}
	
	/**
	 * 删除账户信息
	 */
	@Override
	public void deleteAccount(String loginId) {
		//软删除账户表
		accountDao.softDeleteAccount(SoftdelConstants.DEL_Y, loginId);
		//删除account_accountteam中间表
		accountAteamDao.deleteByAccountId(Long.parseLong(loginId));
		
	}

	/**
	 * 修改账户信息
	 */
	@Override
	public boolean update(UpdateAccountInfo updateAccount) 
	{
		Account account = accountDao.findByUserName(updateAccount.getUserName());
		if ( account !=null &&  !updateAccount.getLoginId().equals(String.valueOf(account.getLoginId()))) {
			//代表该账户已经存在,无法继续修改
			return false;
		}
		//修改账户
		accountDao.updateAccount(updateAccount.getUserName(), updateAccount.getEmailAddr(), updateAccount.getAliasName(),
				updateAccount.getPhone(), updateAccount.getLoginId());
		return true;
	}
	
	/**
	 * 查询账户信息通过登录id
	 * @param loginId
	 * @return
	 */
	public Account findAccountId(String loginId) {
		Account loginAccount = accountDao.findOne(Long.parseLong(loginId));
		log.info("查询到的登录账户信息为:{}",loginAccount);
		return loginAccount;
	}

	/**
	 * 通过手机号和用户名进行分页列表查询
	 */
	@Override
	public List<AccountList> searchList(String userName, String phone, int pageNumber, int pageSize)
	{
		PageHelper.startPage(pageNumber, pageSize);
		
		Map<String, String> pram=new HashMap<>();
		pram.put("userName", userName);//用户名
		pram.put("phone", phone);//联系人
		
		List<AccountList> accountLists = accountMapper.findByUserNameAndLoginId(pram);
		return accountLists;
	}
	
	/**
	 * 查询列表数量
	 */
	@Override
	public int listTotal(String userName, String phone) 
	{
		Map<String, String> pram=new HashMap<>();
		pram.put("userName", userName);//用户名
		pram.put("phone", phone);//联系人
		
		return accountMapper.listTotal(pram);
	}
	
	/**
	 * 通过账户组id查询账户列表
	 */
	@Override
	public List<AccteamList> getAccountListByAteamId(final String ateamId,final Long loginId,final String userName) {
		Map<String, Object> pram=new HashMap<>();
		pram.put("ateamId", ateamId);
		pram.put("userName", userName);
		pram.put("loginId", loginId);
		pram.put("status", 0);
		List<AccteamList> accountList = accountMapper.findAccountByaccountteamId(pram);
		return accountList;
	}
	
    /**
     * 批量删除
     */
	@Override
	public void deleteAccountBatch(AccountBatchInfo accountBatch) {
		List<String> accBatch = accountBatch.getBatchLoginId();
		for(String acc : accBatch) {
			//软删除账户表
			accountDao.softDeleteAccount(SoftdelConstants.DEL_Y, acc);
			//删除account_accountteam中间表
			accountAteamDao.deleteByAccountId(Long.parseLong(acc));
		}
	
	}
	
	//根据用户名loginId查询userName
	public Long findLoginIdByUserName(Long loginId,String userName) {
		Map<String, Object> pram=new HashMap<>();
		pram.put("loginId", loginId);
		pram.put("userName", userName);
		Long id = accountMapper.findLoginIdByUserName(pram);
		return id;
	}

	//软删除
	@Transactional
	public void softDelete(AccountBatchInfo accountBatchInfo) {
		List<String> accBatch = accountBatchInfo.getBatchLoginId();
		//取出最后一个元素得到accountteamId
		String accountteamId = accBatch.get(accBatch.size()-1);
		//删除最后一个元素
		accBatch.remove(accBatch.size()-1);
		for(String acc : accBatch) {
			//根据ID查询账户表
			Account account = findAccountId(acc);
			//account.setStatus("1");
			accountDao.saveAndFlush(account);//修改status=1
			//修改后删除account_accountteam
			accountAteamDao.deleteByAccountteamIdAndAccountId(Long.parseLong(accountteamId), Long.parseLong(acc));
		}
	}

	//修改密码
	public Integer modifypwd(String password,String loginId) {
		//将密码进行md5加密
		String md5password = MD5Util.md5(password);
		return accountDao.modifypwd(md5password,loginId);
	}

	//获取账户表id,name和供应商表id,name(增加账户组时)
	public Map<String, Object> getAccountteamAndSupplier() {
		//获取供应商的id和name
		//获取账户组的id和name
		List<Accountteam> accountteams = accountteamService.findIdAndName();
		Map<String, Object> map=new HashMap<>();
		map.put("accountteams", accountteams);
		return map;
	}

	//禁止用户
	public void forbidAccount(String loginId) {
		//修改为禁止状态0
		accountDao.UpdateAccountStatus(DictConstants.ACCOUNT_NO, loginId);
	}

	/**
	 * 获取消息前获取账户列表
	 */
	@Override
	public List<AccountData> getMsgAccounts() {
		return accountMapper.findAllAccountData();
	}

	//恢复用户
	public void huiFuAccount(String loginId) {
		//修改为正常状态1
		accountDao.UpdateAccountStatus(DictConstants.ACCOUNT_YES, loginId);
		
	}
}
